package com.ebc.eservice.config.handle;

import com.alibaba.fastjson.JSON;
import com.ebc.eservice.util.JsonResult;
import com.ebc.eservice.util.ResponseResult;
import com.ebc.eservice.util.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
    /**
     *  权限验证之前 登录验证
     * @param request
     * @param response
     * @param authException
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {

//        ResponseResult result = new ResponseResult(HttpStatus.UNAUTHORIZED.value(),"用户信息查询失败，请登录");
        JsonResult result = new JsonResult(401,"未授权,请登录",null);
        String json = JSON.toJSONString(result);
        //处理异常
        WebUtils.renderString(response,json);
    }
}
